<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include 'printer.php';

class Storeadmin extends CI_Controller {

  public function __construct()
  {
    parent::__construct();
    
    $this->load->model('group_model');    
    $this->load->helper('url');
    $this->load->library('table');
    $this->load->helper('form');
  }

	public function index()
	{  
    $this->login();
	}

  /**************** 第三方店铺登录辅助函数 ***********/

  private function _3rdLogin($store_key)
  {
    if ($store_key == ""){
      $this->login(True);
      return False;
      // return login failed
    }

    // Invoke model method check exist 
    if ( $this->group_model->existStoreKey($store_key) == False)
    {
      $this->login(True);
      return False;
    }

    $this->session->set_userdata('store_key', $store_key); 
    return True;
  }

  // return key
  // if return False, not login
  private function _3rdGetLogin()
  {
      $store_key = $this->session->userdata('store_key');
      
      if ($store_key == False or $store_key == "")
      {
        redirect(site_url("storeadmin"));
        return "";
      }

      return $store_key;
  }

  // Login 
  public function login($error = False)
  {
      $data["error"] = $error;
      $this->load->view("templates/login/login_header");
      $this->load->view("store_admin/3rd_group_login", $data);
      $this->load->view("templates/login/login_footer");
  }

  public function login_check()
  {
    $data["error"] = False;
    if ($_POST == True){
      $store_key = $this->input->post("store_key"); 
      if ( $this->_3rdLogin($store_key) == True)
        redirect(site_url("storeadmin/grouplist"));
    }
    else
    {
      redirect(site_url("storeadmin"));
    }
  }

  public function logout()
  {
    $this->session->set_userdata('store_key', "");
    redirect(site_url("storeadmin")); 
  }

/**************** group management **************/
  public function grouplist()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['title'] = "团购管理";
    $user_key = $this->_3rdGetLogin();
    if($user_key != '')
    {
      $data['grouplist'] = $this->group_model->get_groups($user_key);
      if($data['grouplist'] != null)
      {
        $data['user'] = 'store';
        $this->load->view("templates/admin/3rd_groupadmin_header", $data);
        $this->load->view('group_admin/grouplist', $data);
        $this->load->view("templates/admin/admin_footer"); 
      }
    } 
  }

  public function groupversionlist()
  {
    if ($this->_3rdGetLogin() == "") return;
    $group_his_id = $this->input->get('group_his_id', TRUE);
    $data['title'] = "历史版本管理";
    $data['grouplist'] = $this->group_model->get_group_versions($group_his_id);
    $data['user'] = 'store';
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('group_admin/groupversionlist', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }
  
  public function newgroup()
  {
    if ($this->_3rdGetLogin() == "") return;
    //deal with pic & info post
    $data['title'] = "添加团购";
    $data['result'] = '';

    if($_POST)
    {
      // todo:处理并存储图片
      $config['upload_path'] = 'uploads/group_pic/logo/';
      //$config['upload_pic_path'] = 'uploads/group_pic/pics';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['encrypt_name']  = TRUE;
      $this->load->library('upload', $config);
     
      if ( ! $this->upload->do_upload('group_logo'))
      {
        $data['error'] = array('error' => $this->upload->display_errors());
        $data['result'] = $data['error']['error'];
      } 
      else
      {
       $res = array('upload_data' => $this->upload->data());
       $data['result'] = $this->group_model->new_group($config['upload_path'] . $res['upload_data']['file_name']);
      }

    }
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('group_admin/newgroup', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function updategroup()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['title'] = "更新团购信息";
    $data['result'] = "";
    $data['command'] = $this->input->get('command', TRUE);
    $group_id = $this->input->get('group_id', TRUE);
    if($_POST)
    {
      // 处理并存储图片
      // 如果没有图片或符合要求的图片，忽略
      // 如果图片存储成功，返回正确的图片存储路径
      $config['upload_path'] = 'uploads/group_pic/logo/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['encrypt_name']  = TRUE;
      $this->load->library('upload', $config);
     
      if ( ! $this->upload->do_upload('group_logo'))
      {
        $data['error'] = array('error' => $this->upload->display_errors());
        $data['result'] = "没有更新图片，因为：" . $data['error']['error'];
        $this->group_model->update_group("");
      } 
      else
      {
       $res = array('upload_data' => $this->upload->data());
       $data['result'] = $this->group_model->update_group($config['upload_path'] . $res['upload_data']['file_name']);
      }
      $old_id = $group_id;
      $group_id = $this->db->insert_id();
      $this->group_model->update_pic($old_id,$group_id);
    }
    $data['group'] = $this->group_model->get_group($group_id);
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('group_admin/updategroup', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function groupdetail()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['title'] = "更新团购信息";
    $data['result'] = "";
    $group_id = $this->input->get('group_id', TRUE);
    $data['group'] = $this->group_model->get_group($group_id);
    $data['user'] = 'store';
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('group_admin/groupdetail', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function deletegroup()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['title'] = "团购删除";
    $this->group_model->delete_group();
    redirect('/groupadmin/grouplist', 'refresh');
  }

  public function backgroup()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['title'] = "团购恢复";
    $this->group_model->back_group();
    redirect('/groupadmin/grouplist', 'refresh');
  }

  /******** bill management *******/
  public function grouphistory()
  {
    if ($this->_3rdGetLogin() == "") return;
    $store_key = $this->_3rdGetLogin();
    if($store_key != "")
    {
      $data['group_bills'] = $this->group_model->get_group_history();
      $data['showdetail'] = false;
      $data['user'] = 'store';
      $this->load->view("templates/admin/3rd_groupadmin_header", $data);
      $this->load->view('group_admin/groupBillList', $data);
      $this->load->view("templates/admin/admin_footer"); 
    }
  }

  public function groupversionhistory()
  {
    if ($this->_3rdGetLogin() == "") return;
    $store_key = $this->_3rdGetLogin();
    if($store_key != "")
    {
      $group_id = $this->input->get('group_id',TRUE);
      $group_his_id = $this->input->get('group_his_id',TRUE);
      if($group_id != null)
      {
        $data['group_bills'] = $this->group_model->get_group_ver_history($group_id,1);
        $data['group'] = $this->group_model->get_group($group_id);
        $data['user'] = 'store';
      }
      else if($group_his_id != null) 
      {
        if(!$this->group_model->haveKey($group_his_id,$store_key,1))
        {
          //redirect('/storeadmin/grouplist', 'refresh');
          $this->store_key_fail('查看订单','订单不存在或您无权查看，请返回。');
          return;
        }
        $data['group_bills'] = $this->group_model->get_group_ver_history($group_his_id,2);
        $data['user'] = 'history';
      }
      else
        return;
      $data['showdetail'] = false;
      $data['title'] = "团购历史";
      $data['group_his_id'] = $group_his_id;
      $this->load->view("templates/admin/3rd_groupadmin_header", $data);
      $this->load->view('group_admin/versiongroupBillList', $data);
      $this->load->view("templates/admin/admin_footer"); 
    }
  }

  public function historydetail()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['bills'] = $this->gen_bill_model->get_bills();
    $data['showdetail'] = true;
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('billlist', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function usebill()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['title'] = "订单使用";
    $this->group_model->use_bill();
    $group_his_id = $this->input->get('group_his_id', TRUE);
    redirect('/storeadmin/groupversionhistory?group_his_id='.$group_his_id, 'refresh');
  }

  public function backbill()
  {
    if ($this->_3rdGetLogin() == "") return;
    $data['title'] = "订单恢复";
    $this->group_model->back_bill();
    $group_his_id = $this->input->get('group_his_id', TRUE);
    redirect('/storeadmin/groupversionhistory?group_his_id='.$group_his_id, 'refresh');
  }

  // public function check()
  // {
  //   $interval_time_const = 150;//150s = 2.5min
  //   $unprint_bills = $this->gen_bill_model->get_unprint_all_bills();
  //   $num = 0;
  //   $content = "";
  //   foreach ($unprint_bills as $bills) 
  //   {
  //     $bill_time = $bills['time'];
  //     $bill_time = intval($bill_time);
  //     $now_time = time();
  //     $interval_time = $now_time-$bill_time;
  //     if ($interval_time > $interval_time_const)
  //     {       
  //       $num = $num + 1;
  //       $json = json_decode($bills['bill_json'],TRUE);
  //       $store_id = $json['store_id'];
  //       $user_name = $bills['name'];
  //       $bill_id = $bills['bill_id'];
  //       $this->gen_bill_model->update_send_email($bill_id);
  //       $phone = $bills['phone'];
  //       $address = $bills['address'];
  //       $note = $bills['note'];
  //       $time = $bills['time'];
  //       $store_info = $this->store_model->get_store($store_id);
  //       $store_name = $store_info[0]['store_name'];
  //       $bill_total_price = $json['total_price'];
  //       $list = $json['list'];

  //       $interval_time_min = round($interval_time/60);
  //       $interval_time_sec = $interval_time%60;
  //       $content = $content."已经超时:".(string)$interval_time_min."分".(string)$interval_time_sec."秒"
  //         ."\n订购姓名：" . $user_name 
  //         ."\n订单编号：" . $bill_id
  //         ."\n联系电话：" . $phone
  //         ."\n送货地址：" . $address
  //         ."\n外卖备注：" . $note
  //         ."\n下单时间：" . date('Y-m-d H:i:s', $time)
  //         ."\n外卖店铺：" . $store_name
  //         ."\n外卖总价：" . $bill_total_price
  //         ."\n\n订单详情：";

  //       foreach ($list as $item):
  //         $content .= "\n" . $item['item_name'] . "\t\t" . $item['item_price'] . "\t\t" . $item['item_account'];
  //       endforeach;
  //       $divider = "\n--------------------------------------------------------\n\n\n\n";
  //       $content = $content.$divider;
  //       //echo mb_convert_encoding($content,"GBK", "UTF-8");  

  //     }
  //   }
  //   if ($num == 0)
  //     echo "There is not exist a bill that is not printed!".(string)(time());
  //   else
  //   {
  //     $content = "请注意:以下订单已经下单超过2.5分钟还未被打印出来!"
  //             ."\n请及时联系店铺老板！"
  //             ."\n一共有".(string)$num."张订单"
  //             ."\n\n\n下面是未打印的订单:\n\n\n".$content;
  //     $store_mail = "tongshensi1992@163.com";
  //     $title = "紧急情况:超时订单!!!!";
  //     $this->send2mail($store_mail, $title, $content);
  //     echo mb_convert_encoding($content,"GBK", "UTF-8");
  //   }
  // }

  /***************** bill management end *****************/


  /**************** pic management ****************/
  /* table : item 
    `item_id` int(11) NOT NULL AUTO_INCREMENT,
    `item_name` varchar(128) NOT NULL,
    `item_type` varchar(128) NOT NULL,
    `item_price` varchar(128) NOT NULL,
    `item_note` varchar(256) DEFAULT NULL,
    `item_pic` varchar(256) NOT NULL,
    `store_id` int(11) NOT NULL,
    `item_state` int(11) NOT NULL,
  */

  public function piclist()
  {
    if ($this->_3rdGetLogin() == "") return;
    $group_id = $this->input->get('group_id', TRUE);
    $data['group_id'] = $group_id;
    $data['title'] = '团购图片管理（团购ID:' . $group_id . "）";
    $data['piclist'] = $this->group_model->get_all_pics($group_id);
    $data['user'] = 'store';
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('group_admin/piclist', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function picdetail()
  {
    if ($this->_3rdGetLogin() == "") return;
    $group_id = $this->input->get('group_id', TRUE);
    $data['group_id'] = $group_id;
    $data['title'] = '团购图片管理（团购ID:' . $group_id . "）";
    $data['group'] = $this->group_model->get_group($group_id);
    $data['piclist'] = $this->group_model->get_all_pics($group_id);
    $data['user'] = 'store';
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('group_admin/versionpiclist', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function newpic()
  {
    if ($this->_3rdGetLogin() == "") return;
    $group_id = $this->input->get('group_id', TRUE);
    if ($group_id == "")
    {
      redirect('/groupadmin/', 'refresh');
    }
    //deal with pic & info post
    $data['group_id'] = $group_id;
    $data['title'] = "添加图片（商店ID:" . $group_id . "）";
    $data['result'] = '';

    if($_POST)
    {
      // todo:处理并存储图片
      $config['upload_path'] = 'uploads/group_pic/pics/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['encrypt_name']  = TRUE;
      $this->load->library('upload', $config);
     
      if ( ! $this->upload->do_upload('group_pic'))
      {
        $data['error'] = array('error' => $this->upload->display_errors());
        $data['result'] = "因为选择了显示封面图片，提交时必须上传图片：" . $data['error']['error'];
      } 
      else
      {
        $res = array('upload_data' => $this->upload->data());
        $this->group_model->new_pic($config['upload_path'] . $res['upload_data']['file_name'], $group_id);
        $data['result'] = '添加成功';
      }

    }  
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('group_admin/newpic', $data);  
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function updateitem()
  {
    if ($this->_3rdGetLogin() == "") return;
    $store_id = $this->input->get('store_id', TRUE);
    $item_id  = $this->input->get('item_id', TRUE);
    if ($store_id == "" or $item_id == "")
    {
      redirect('/groupadmin/storelist', 'refresh');
    }

    $data['group_id'] = $group_id;
    $data['title'] = "添加图片（团购ID:" . $group_id . "）";
    $data['result'] = '';

    // create new item
    if($_POST)
    {
      // delete old item
      $this->item_model->delete_item($item_id, $store_id);

      // todo:处理并存储图片
      $config['upload_path'] = 'uploads/item_pic/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['encrypt_name']  = TRUE;
      
      $this->load->library('upload', $config);
     
      if ( ! $this->upload->do_upload('item_pic'))
      {
        $data['error'] = array('error' => $this->upload->display_errors());       
        $tmp_item = $this->item_model->get_item($item_id);

        if ($tmp_item[0]["item_pic"] == "" and $this->input->post('show_pic') == 'show')
        {
            $data['result'] = "原图片不存在且选择显示图片，必需上传图片:" . $data['error']['error'];
        }
        else
        {
            $data['result'] = "除图片外其他信息更新成功:" . $data['error']['error'];
            $item_id = $this->item_model->new_item($tmp_item[0]["item_pic"], $store_id);
        }        
      } 
      else
      {
        $res = array('upload_data' => $this->upload->data());
        $item_id = $this->item_model->new_item($config['upload_path'] . $res['upload_data']['file_name'], $store_id);
      }
    }

    // return new item info
    $data['item'] = $this->item_model->get_item($item_id);
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('admin/updateitem', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  public function hidepic()
  {
    if ($this->_3rdGetLogin() == "") return;
    $group_id = $this->input->get('group_id', TRUE);
    $pic_id  = $this->input->get('pic_id', TRUE);
    $this->group_model->hide_pic($pic_id);
    redirect('/groupadmin/piclist?group_id='.$group_id, 'refresh');    
  }

  public function backpic()
  {
    if ($this->_3rdGetLogin() == "") return;
    $group_id = $this->input->get('group_id', TRUE);
    $pic_id  = $this->input->get('pic_id', TRUE);
    $this->group_model->back_pic($pic_id);
    redirect('/groupadmin/piclist?group_id='.$group_id, 'refresh');    
  }

  public function deletepic()
  {
    if ($this->_3rdGetLogin() == "") return;
    $group_id = $this->input->get('group_id', TRUE);
    $pic_id  = $this->input->get('pic_id', TRUE);
    $this->group_model->delete_pic($pic_id);
    redirect('/groupadmin/piclist?group_id='.$group_id, 'refresh');    
  }
  /**************** item management end ************/  

  private function store_key_fail($title,$reason)
  {
    if ($this->_3rdGetLogin() == "") return;
    // 返回
    $data['user'] = 'store';
    $data['title'] = $title;
    $data['reason'] = $reason;
    $data['backurl'] = 'storeadmin/grouplist';
    $this->load->view("templates/admin/3rd_groupadmin_header", $data);
    $this->load->view('store_admin/fail', $data);
    $this->load->view("templates/admin/admin_footer"); 
  }

  // public function printer()
  // {
  //   $Printer = NetPrinterClass::getInstance(); 

  //   if (isset($Printer->params['id']) && isset($Printer->params['sta']))  // 返回打印结果
  //   {
  //     $sta = $this->input->get('sta',TRUE);
  //     $id = $this->input->get('id',TRUE);
  //     if ($sta == "0")//打印成功
  //     {
  //       $this->gen_bill_model->update_print_state($id);
  //     }
  //     else if ($sta == "1" || $sta == "3")//过热 or 缺纸卡纸
  //     {
  //       $IMEI = $this->input->get('usr', TRUE);
  //       $temp_store_id = $this->store_model->get_store_id($IMEI);
  //       $store_id = $temp_store_id['store_id'];

  //       $store_mail = $this->store_model->get_store_mail($store_id);
  //       $title = "";
  //       if ($sta == "1")
  //       {
  //         $title = "紧急情况:打印机故障-过热！！";
  //       }
  //       else 
  //       {
  //         $title = "紧急情况:打印机故障-缺纸卡纸！！";
  //       }
  //       $unprint_bills = $this->gen_bill_model->get_unprint_bills($store_id);

  //       $content = "下面是未打印的订单:\n\n\n";
  //       foreach ($unprint_bills as $bills) {
  //         $user_name = $bills['name'];
  //         $bill_id = $bills['bill_id'];
  //         $phone = $bills['phone'];
  //         $address = $bills['address'];
  //         $note = $bills['note'];
  //         $time = $bills['time'];
  //         $store_info = $this->store_model->get_store($store_id);
  //         $store_name = $store_info[0]['store_name'];
  //         $json = json_decode($bills['bill_json'],TRUE);
  //         $bill_total_price = $json['total_price'];
  //         $list = $json['list'];

  //         $content = $content."订购姓名：" . $user_name 
  //           ."\n订单编号：" . $bill_id
  //           ."\n联系电话：" . $phone
  //           ."\n送货地址：" . $address
  //           ."\n外卖备注：" . $note
  //           ."\n下单时间：" . date('Y-m-d H:i:s', $time)
  //           ."\n外卖店铺：" . $store_name
  //           ."\n外卖总价：" . $bill_total_price
  //           ."\n\n订单详情：";

  //         foreach ($list as $item):
  //           $content .= "\n" . $item['item_name'] . "\t\t" . $item['item_price'] . "\t\t" . $item['item_account'];
  //         endforeach;
  //         $divider = "\n----------------------------\n";
  //         $content = $content.$divider;
  //       }
  //       //echo mb_convert_encoding($content,"GBK", "UTF-8");  
  //       $this->send2mail($store_mail, $title, $content);
  //     }
  //     else//未知错误
  //     {
  //     } 
  //   }
  //   else // 传输需要打印的内容
  //   {   
  //     $IMEI = $this->input->get('usr', TRUE);
  //     // echo $IMEI;
  //     if ($IMEI == "")//如果机器码为空
  //     {
  //       echo null;
  //     }
  //     else//机器码正常
  //     {
  //       // echo $IMEI;
  //       $temp_store_id = $this->store_model->get_store_id($IMEI);
  //       if ($temp_store_id == NULL)//如果该机器不属于任何一个商铺
  //       {
  //           $content = (string)$IMEI." not exist";
  //       }
  //       else//如果机器属于store_id商铺
  //       {
  //         $store_id = $temp_store_id['store_id'];
  //         $last_unprint_bill = $this->gen_bill_model->get_last_unprint_bill($store_id);
  //         if ($last_unprint_bill == null)//如果没有待打印订单
  //         {
  //           $bill_id = 0;
  //           $content = "store_id ".(string)$store_id." not exist";
  //         }
  //         else
  //         {
  //           $temp_content = $last_unprint_bill['bill_json'];
  //           //$content = "名称      数量%%";
  //           $json = json_decode($temp_content,TRUE);
  //           $content_list = $json["list"];

  //           //$content = $content_list;
  //           $name_length_const = 12;
  //           $number_length_const = 5;
  //           $price_length_const = 5;
  //           $total_price_length_const = 5;//常量定义
  //           $divider = "----------------------------%%";

  //           $content = "%30聚惠家外卖单%%";

  //           $store_info = $this->store_model->get_store($store_id);       
  //           $store_name = $store_info[0]['store_name'];
  //           $store_name = "%30".$store_name."%%";
  //           $content = $content.$store_name;//取店铺名称

  //           $character_time = "%00下单时间:";
  //           $time = $last_unprint_bill['time'];
  //           $time = date('Y-m-d H:i:s', $time);
  //           $content = $content.$character_time.$time."%%";//取下单时间

  //           $character_bill_id = "订单编号:";
  //           $bill_id = $last_unprint_bill['bill_id'];
  //           $content = $content.$character_bill_id.$bill_id."%%";//订单编号

  //           $content = $content.$divider;

  //           $character_name = "名称";
  //           $character_name = str_pad($character_name,$name_length_const+2,".");
  //           $character_number = "数量";
  //           $character_number = str_pad($character_number,$number_length_const+2,".",STR_PAD_LEFT);
  //           $character_price = "单价";
  //           $character_price = str_pad($character_price,$price_length_const+2,".",STR_PAD_LEFT);
  //           $character_total_price = "金额";
  //           $character_total_price = str_pad($character_total_price,$total_price_length_const+2,".",STR_PAD_LEFT);
  //           $content = $content.$character_name.$character_number.$character_price.$character_total_price."%%";

  //           foreach ($content_list as $cl) {
  //             $name = $cl["item_name"];//名称
  //             $name_length = strlen($name);
  //             if ($name_length*2/3 > $name_length_const)
  //             {
  //               $name = substr($name, 0,$name_length_const*3/2);
  //               $name_length = $name_length_const*3/2;
  //             }
  //             $name = str_pad($name,$name_length_const+$name_length/3,".");

  //             $account = $cl["item_account"];//数量
  //             $int_account = intval($account);//数量转int
  //             $account = str_pad($account,$number_length_const,".",STR_PAD_LEFT);

  //             $item_price = $cl["item_price"];//该物品单价
  //             $float_price = floatval($item_price);//单价转float
  //             $float_price = number_format($float_price,1,'.','');//格式化
  //             $item_price = str_pad($float_price,$price_length_const,".",STR_PAD_LEFT);

  //             $item_total_price = $float_price*$int_account;//计算得总价
  //             $item_total_price = number_format($item_total_price,1,'.','');//格式化
  //             $item_total_price = str_pad($item_total_price,$total_price_length_const,".",STR_PAD_LEFT);

  //             $temp = $name.$account.$item_price.$item_total_price."%%";
  //             $content = $content.$temp;
  //           }
  //           //$content = $temp_content;
  //           $content = $content.$divider;

  //           $character_all_total_price = "合计:";
  //           $total_price = $json['total_price'];
  //           $total_price = floatval($total_price);
  //           $total_price = number_format($total_price,1,'.','');
  //           $character_all_total_price = $character_all_total_price.(string)$total_price;
  //           $content = $content.$character_all_total_price."%%";//合计金额

  //           $character_user_name = "姓名:";
  //           $user_name = $last_unprint_bill['name'];
  //           $content = $content.$character_user_name.$user_name."%%";//顾客姓名

  //           $character_address = "地址:";
  //           $adress = $last_unprint_bill['address'];
  //           $content = $content.$character_address.$adress."%%";//地址

  //           $character_phone_number = "电话:";
  //           $phone_number = $last_unprint_bill['phone'];
  //           $content = $content.$character_phone_number.$phone_number."%%";//电话

  //           $character_note = "备注:";
  //           $note = $last_unprint_bill['note'];
  //           $content = $content.$character_note.$note."%%";//备注

  //           $logo = "Powered by 聚惠家";
  //           $erweima = "%50142Bhttp://weixin.qq.com/r/6UxrcznEHGRjrX0Q9xn7%%";
  //           $content = $content.$erweima.$logo;

  //           // $content = $content."%%%%%%%%%%%%%%%%".$content;
  //           $time = $last_unprint_bill['time'];

  //           //$bill_id = $last_unprint_bill['bill_id'];
  //           $toPrint = $Printer->setId($bill_id) // 设置ID
  //                   ->setTime($time) // 设置时间
  //                   ->setContent($content) // 设置content
  //                   // ->setSetting("101:6|105:0") // 设置打印机参数等数据，具体参考协议部分文件，建议非必要不要设置，也可以为空
  //                   ->display(); // 输出
  //           // echo mb_convert_encoding($bill_id,"GBK", "UTF-8");  
  //           echo mb_convert_encoding($toPrint,"GBK", "UTF-8");  
  //         }
  //       } 
  //     }
  //   }
  // }

  // public function send2mail($mail, $title = "新订单", $content = "")
  // {
  //   $config['protocol'] = 'smtp';
  //   $config['charset'] = 'utf-8';
  //   $config['wordwrap'] = TRUE;
  //   $config['smtp_host'] = 'smtp.163.com';
  //   $config['smtp_user'] = 'juhuijiainfo@163.com';
  //   $config['smtp_pass'] = 'juhuijiainfo123';

  //   $this->load->library('email');

  //   $this->email->initialize($config);


  //   $this->email->from('juhuijiainfo@163.com', '刘晓宁');
  //   $this->email->to('769904450@qq.com,'.'juhuijiainfo@163.com,'.$mail); //注意，部署之前需要将公邮打开

  //   $this->email->subject($title);
  //   $this->email->message($content); 

  //   $this->email->send();

  // //echo $this->email->print_debugger();
  // }

  
}
